//进行数据脱敏处理，手机号码、姓名、证件号码
export function desensitizedData(data) {
  let dataArray = JSON.parse(data)
  dataArray = dataArray.map(d => {
    //脱敏手机号码
    d.faketel = d.tel.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
    //脱敏姓名
    d.fakexm = noPassByName(d.xm)
    //脱敏证件号码

    // d.idcard = d.idcard.replace(/(\d{4})\d*(\d{3})/,'$1**************$2')
    d.fakeidcard = desensitizedZjhm(d.idcard)
    if (d.uploadpicture != '') {
      var nowDate = new Date()
      let picObj = JSON.parse(d.uploadpicture)
      for (let key in picObj) {
        d[key] = [{ url: `${picObj[key]}?${nowDate.getTime()}`, isImage: true }]
      }
      d.uploadpicture = picObj
    } else {
      d.uploadpicture = {}
    }
    return d
  })
  return JSON.stringify(dataArray)
}

//脱敏姓名
export function noPassByName(str) {
  if (null != str && str != undefined) {
    if (str.length <= 3) {
      return '*' + str.substring(1, str.length)
    } else if (str.length > 3 && str.length <= 6) {
      return '**' + str.substring(2, str.length)
    } else if (str.length > 6) {
      return str.substring(0, 2) + '****' + str.substring(6, str.length)
    }
  } else {
    return ''
  }
}

//脱敏证件号码
export function desensitizedZjhm(val) {
  if (val) {
    if (val.length) {
      const len = val.length
      if (len === 1) {
        return '*'
      } else if (len === 2) {
        return val[0] + '*'
      } else if (len === 3) {
        return val[0] + '*' + val[2]
      } else if (len > 3 && len < 6) {
        const newVal = val.substring(2, len - 1)
        let newValChange = []
        for (let i = 0; i < newVal.length; i++) {
          newValChange.push('*')
        }
        return val[0] + val[1] + newValChange.join('') + val[len - 1]
      } else if (len >= 6 && len < 8) {
        const newVal = val.substring(2, len - 2)
        let newValChange = []
        for (let i = 0; i < newVal.length; i++) {
          newValChange.push('*')
        }
        return val[0] + val[1] + newValChange.join('') + val[len - 2] + val[len - 1]
      } else if (len >= 8 && len < 15) {
        const newVal = val.substring(4, len - 2).replace(/\d/g, '*')
        return val.substring(0, 4) + newVal + val.substring(val.length - 2, val.length)
      } else if (len > 15) {
        const newVal = val.substring(5, len - 4).replace(/\d/g, '*')
        return val.substring(0, 6) + newVal + val.substring(val.length - 3, val.length)
      }
    }
  }
}
